﻿using Main;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.AddressableAssets.ResourceLocators;
using UnityEngine.ResourceManagement.ResourceLocations;
using UnityEngine;

public class AddressablesManager : Singleton<AddressablesManager>
{
    private string prefixURL = "";
    public AsyncOperationHandle<IResourceLocator> InitializeAsync(string URL = null)
    {
        prefixURL = string.IsNullOrWhiteSpace(URL) ? Application.streamingAssetsPath : URL;
        Addressables.InternalIdTransformFunc = InternalIdTransformFunc;
        return Addressables.InitializeAsync(true);
    }
    private string InternalIdTransformFunc(IResourceLocation location)
    {
        // 默认所有资源都是放在StreamingAssets目录下
        string pattern = ".+StreamingAssets(/AssetBundle.+|/aa.+)";
        Match match = Regex.Match(location.InternalId, pattern);
        if (match.Success)
        {
            return prefixURL + match.Groups[1].Value;
        }
        else
        {
            return location.InternalId;
        }
    }
}
